<template>
  <div class="box">
    <header></header>
    <div class="top">
      <div>
        <router-link to="/personal">
          <span class="iconfont iconjiantou2"></span>
        </router-link>
      </div>
      <div>编辑资料</div>
    </div>
    <div class="top_img">
      <img v-if="val.head_img" :src=" $axios.defaults.baseURL + val.head_img" alt />
      <img v-else src="@/assets/logo.png" alt />
      <!-- 上传图片 afterRead 函数会传入图片信息 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <div class="account" v-if="val">
      <div class="account_" @click="nickshow = true">
        <div class="add">昵称</div>
        <div class="btn">
          <samp href="javascript:;">
            {{val.nickname}}
            <span class="iconfont iconjiantou1"></span>
          </samp>
        </div>
      </div>
      <div class="account_" @click="passshow = true">
        <div class="add">密码</div>
        <div class="btn">
          <samp href="javascript:;">
            ******
            <span class="iconfont iconjiantou1"></span>
          </samp>
        </div>
      </div>
      <div class="account_" @click="gendershow = true">
        <div class="add">性别</div>
        <div class="btn">
          <samp href="javascript:;">
            {{val.gender == 1 ? '男' : '女'}}
            <span class="iconfont iconjiantou1"></span>
          </samp>
        </div>
      </div>
    </div>

    <van-dialog v-model="nickshow" title="更改昵称" show-cancel-button @confirm="nameclick">
      <!-- 输入任意文本 -->
      <van-field v-model="nick" label="昵称" />
    </van-dialog>

    <van-dialog v-model="passshow" title="更改密码" show-cancel-button @confirm="passclick">
      <!-- 允许输入数字，调起带符号的纯数字键盘 -->
      <van-field v-model="nick" type="number" label="数字" />
    </van-dialog>

    <van-action-sheet
        v-model="gendershow"
        :actions="actions"
        cancel-text="取消"
        close-on-click-action
        @cancel="onCancel"
        @select="setGender"
    />
  </div>
</template>

<script>
export default {
    data(){
        return{
            val:{},
            nickshow:false,
            nick:'',
            passshow:false,
            //pass:''
            gendershow:false,
            actions:[
                {name:'男',gender:1},{name:'女',gender:0}
            ]
        }
    },
    created(){
       
        this.Request()
    },
    methods:{
        
        Request(){
            this.$axios({  //带上token和userid请求数据
            url:'/user/' + localStorage.getItem('userid'),
            headers:{
                // 在 token 前添加 'Bearer '
                Authorization: 'Bearer '+ localStorage.getItem('token')
            },
            }).then((res)=>{
                console.log(res);
                if(res.data.message == '获取成功'){
                    this.val = res.data.data
                }
                //console.log(res.data.data.head_img);
            })
        },

        CompileRequest(val){  //封装请求函数
           this.$axios({
               url:'/user_update/'+ localStorage.getItem('userid'),
               method:'post',
               data:val,
               headers:{
                   Authorization:"Bearer " + localStorage.getItem('token')
               }
           }).then(res=>{
               console.log(res);

               this.Request()
               this.nick = ''
           })
        },
        nameclick(){  //编辑昵称
            this.CompileRequest({nickname:this.nick})
        },
        passclick(){  //编辑密码
            this.CompileRequest({password:this.nick})
            
        },
        onCancel(){  //取消编辑性别
            this.$toast('取消编辑')
        },
        setGender(val){ //编辑性别
            console.log('编辑性别:'+val.gender);
            const gen = val.gender
            this.CompileRequest({gender:gen})
        },

        afterRead(files){  //文件上传
            let fi = new FormData()
            fi.append('file',files.file)
            //console.log(files.file);
            this.$axios({
                url:'/upload',
                method:'post',
                data:fi,
                headers:{
                    Authorization:'Bearer ' + localStorage.getItem('token')
                }
            }).then(res=>{
                let {data,message} = res.data
                console.log(res);
                if(message == '文件上传成功'){

                    this.CompileRequest({head_img:data.url})  //上传成功后调用修改图片函数
                }
            })
        }
    }
};
</script>

<style lang="less" scoped>
.box {
  font-size: 15/360 * 100vw;
  font-weight: 400;
  background: #f2f2f2;
  width: 100%;
  height: 100%;
  header {
    height: 20/360 * 100vw;
    background-color: #757575;
  }
  .top {
    display: flex;
    line-height: 35/360 * 100vw;
    div {
      font-weight: 700;
    }
    div:nth-child(1) {
      width: 37%;
      text-align: left;
      padding-left: 15/360 * 100vw;
    }
  }
  .top_img {
    height: 120/360 * 100vw;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20/360 * 100vw;
    position: relative;
    img {
      //图片部分
      width: 90/360 * 100vw;
      height: 90/360 * 100vw;
      border-radius: 50%;
      object-fit: cover;
    }
    .van-uploader{
        position: absolute;
        opacity: 0;
    }
  }
  .account {
    display: flex;
    flex-direction: column;
    //width: 100%;
    padding-left: 15/360 * 100vw;
    line-height: 40/360 * 100vw;
    .account_ {
      display: flex;
      justify-content: space-between;
      border-bottom: solid 1px #c4c4c4;
      .btn {
        color: #a19e9e;
        padding-right: 15/360 * 100vw;
      }
    }
  }
}
</style>